package com.shopx.cm.service.impl;


import com.shopx.cmm.DTO.PageDTO;
import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.exception.OriginException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.base.service.base.BaseService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.shopx.core.common.CommonConstant;
import com.shopx.dal.redis.RedisCacheService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import com.shopx.dal.redis.CacheServiceFactory;
import com.shopx.cm.bean.CmBrandCk;
import com.shopx.cm.model.CmBrandCkModel;
import com.shopx.cm.dao.db1.CmBrandCkDao;
import com.shopx.cm.service.ICmBrandCkService;

/**
 * TODO
 *
 * @author lituo
 * @className: CmBrandCk
 * @date 2025-02-21
 */
@Service
@Slf4j
public class CmBrandCkServiceImpl implements ICmBrandCkService {

    @Resource
    private CmBrandCkDao cmBrandCkDao;

    @Resource
    private ISysSeqService sysSeqService;

    @Resource
    private RedisCacheService redisCacheService;


    @Override
    public CmBrandCkModel selectByPrimaryKey(CmBrandCk cmBrandCk) {
        if (cmBrandCk == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        return cmBrandCkDao.selectByPrimaryKey(cmBrandCk);
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int insertSelective(CmBrandCk cmBrandCk) {
        if (cmBrandCk == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        String id = sysSeqService.getTabSeq("CmBrandCk", 4, null);
        cmBrandCk.setCbcId(id);
        int result = cmBrandCkDao.insertSelective(cmBrandCk);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(CmBrandCk cmBrandCk) {
        if (cmBrandCk == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmBrandCkDao.updateByPrimaryKeySelective(cmBrandCk);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int deleteByPrimaryKey(CmBrandCk cmBrandCk) {
        if (cmBrandCk == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmBrandCkDao.deleteByPrimaryKey(cmBrandCk);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    public int selectCount(CmBrandCkModel cmBrandCk) {
        return cmBrandCkDao.selectCount(cmBrandCk);
    }

    @Override
    public List<CmBrandCkModel> selectList(CmBrandCkModel cmBrandCk) {
        List<CmBrandCkModel> list = cmBrandCkDao.selectList(cmBrandCk);
        return list;
    }

    @Override
    public Map<String, CmBrandCkModel> selectListByCode(){
        Object o = redisCacheService.get(CommonConstant.CacheCode.CK_CODE);
        if(o==null){
            List<CmBrandCkModel> list = cmBrandCkDao.selectList(null);
            Map<String, CmBrandCkModel> map = new HashMap<>();
            for(CmBrandCkModel model:list){
                map.put(model.getCkCode(),model);
            }
            redisCacheService.set(CommonConstant.CacheCode.CK_CODE,map,7200);
            return map;
        }
        return (Map<String, CmBrandCkModel>)o;
    }

    @Override
    public Map<String, CmBrandCkModel> selectListByMaterial(){
        Object o = redisCacheService.get(CommonConstant.CacheCode.CK_MATERIAL);
        if(o==null){
            List<CmBrandCkModel> list = cmBrandCkDao.selectList(null);
            Map<String, CmBrandCkModel> map = new HashMap<>();
            for(CmBrandCkModel model:list){
                map.put(model.getBrandName(),model);
            }
            redisCacheService.set(CommonConstant.CacheCode.CK_MATERIAL,map,7200);
            return map;
        }
        return (Map<String, CmBrandCkModel>)o;
    }



    @Override
    public PageDTO<CmBrandCkModel> getListPage(CmBrandCkModel cmBrandCk) {
        if (cmBrandCk == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        PageDTO<CmBrandCkModel> pageDTO = new PageDTO<CmBrandCkModel>();
        pageDTO.setiTotalDisplayRecords(selectCount(cmBrandCk));
        pageDTO.setData(selectList(cmBrandCk));
        return pageDTO;
    }

}

